<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------

namespace app\supplier\controller;
use think\Session;
use app\supplier\controller\Base;
use app\supplier\model\FieldModel;
use app\supplier\model\CountryModel;
use app\supplier\model\MakeModel;
use app\supplier\model\SeriesModel;
use app\supplier\model\MatorModel;
use app\supplier\model\ProductModel;
use app\supplier\model\PtypeModel;

class Mator extends Base
{
	/**
	 * 制造商对象
	 * @return mixed
	 */
	public $make_obj;
	
	/**
	 * 国家对象
	 * @return mixed
	 */
	public $country_obj;
	
	
	/**
	 * 系列对象
	 * @return mixed
	 */
	public $series_obj;
	
	/**
	 * 车型对象
	 * @return mixed
	 */
	public $mator_obj;
	
	/**
	 * 产品对象
	 * @return mixed
	 */
	public $product_obj;
	
	/**
	 * 产品类型对象
	 * @return mixed
	 */
	public $ptype_obj;
	
	/**
	 * 构造函数
	 * @return mixed
	 */
	public function __construct()
	{
		parent::__construct();
		$this->product_obj = new ProductModel(); 
		$this->ptype_obj = new PtypeModel(); 
		$this->make_obj = new MakeModel();
		$this->series_obj = new SeriesModel();	
		$this->mator_obj = new MatorModel();
		$this->country_obj = new CountryModel();
	}
	
	/**
	 * 车型列表
	 * @return mixed
	 */
    public function lists()
    {
		$fieldData = array();
		$field = FieldModel::all();
		foreach($field as $_f){
			$fieldData[$_f['id']] = $_f;
		}
		$countryData = array();
		$country = CountryModel::all();
		foreach($country as $_c){
			$countryData[$_c['id']] = $_c;
		}
		$makeData = array();
		$make = MakeModel::all();
		foreach($make as $_m){
			$makeData[$_m['id']] = $_m;
		}
		$seriesData = array();
		$series = SeriesModel::all();
		foreach($series as $_s){
			$seriesData[$_s['id']] = $_s;
		}
		$input = input('get.');
		if(isset($input['data'])){
			$input['supplier_id'] = Session::get('supplier_id');
			$data = $this->mator_obj->getMatorSkuList($input);
			if(!empty($data['list']['data'])){
				foreach($data['list']['data'] as $key=>$val){
					$data['list']['data'][$key]['field_country_code'] = $fieldData[$val['field_id']]['field_code'].'-'.$countryData[$val['country_id']]['country_code'];
					$data['list']['data'][$key]['field_country_name'] = $fieldData[$val['field_id']]['field_name'].'-'.$countryData[$val['country_id']]['country_name'];
					$data['list']['data'][$key]['make_name'] = $makeData[$val['make_id']]['make_name'];
					$data['list']['data'][$key]['series_name'] = $seriesData[$val['series_id']]['series_name'];
					$data['list']['data'][$key]['year_name'] = '';
					if($seriesData[$val['series_id']]['from_time'] > 0){
						$data['list']['data'][$key]['year_name'] = date('Y/m',$seriesData[$val['series_id']]['from_time']).'~'.date('Y/m',$seriesData[$val['series_id']]['to_time']);
					}
					if(is_numeric($val['create_time'])){
						$data['list']['data'][$key]['create_time'] = date('Y-m-d H:i:s',$val['create_time']);
					}
				}
			}
			$result = array('code'=>0,'msg'=>'','count'=>$data['list']['total'],'data'=>$data['list']['data']);
			return json($result);
		}else{
			$where = array();
			$where['export'] = 1;
			$data = $this->country_obj->getFieldCountryList($where);
			$role_ids = Session::get('role_ids');
			if(!empty($data['list'])){
				foreach($data['list'] as $key=>$val){
					if(!in_array($val['id'],$role_ids)){
						unset($data['list'][$key]);
						continue;
					}
					$fieldObj = FieldModel::get($val['field_id']);
					$countryObj = CountryModel::get($val['country_id']);
					$data['list'][$key]['field_country_code'] = $fieldObj->field_code." - ".$countryObj->country_code;
					$data['list'][$key]['field_country_name'] = $fieldObj->field_name." - ".$countryObj->country_name;
				}
			}
			$this->assign('field_country',$data['list']);
			$this->assign('field', $fieldData);
			return view('lists');
		}
    }
	
	/**
	 * 车型列表
	 * @return mixed
	 */
    public function product()
    {
		$ptype = $this->ptype_obj->getPtypeList(['export'=>1]);
		$psale = config('product_sale_list');
		$input = input('get.');
		if(isset($input['data'])){
			$supplier_id = Session::get('supplier_id');
			$input['supplier_id'] = $supplier_id;
			$input['product_ids'] = array();
			$product_rows = $this->mator_obj->getSupplierProductSku(array('mator_id'=>$input['mator_id']));
			foreach($product_rows as $_p){
				array_push($input['product_ids'],$_p['product_id']);
			}
			$data = $this->product_obj->getProductList($input);
			if(!empty($data['list']['data'])){
				$ptype_arr = array();
				foreach($ptype['list'] as $_p){
					$ptype_arr[$_p['id']] = $_p['type_name'];
				}
				foreach($data['list']['data'] as $key=>$val){
					if(is_numeric($val['create_time'])){
						$data['list']['data'][$key]['create_time'] = date('Y-m-d H:i:s',$val['create_time']);
					}
					$data['list']['data'][$key]['type_name'] = $ptype_arr[$val['type_id']];
					$data['list']['data'][$key]['sale_name'] = $psale[$val['product_sale']];
				}
			}
			$result = array('code'=>0,'msg'=>'','count'=>$data['list']['total'],'data'=>$data['list']['data']);
			return json($result);
		}else{
			$this->assign('input',$input);
			return view('product');
		}
	}
	
	/**
	 * 车型列表
	 * @return mixed
	 */
    public function slists()
    {
		$fieldData = array();
		$field = FieldModel::all();
		foreach($field as $_f){
			$fieldData[$_f['id']] = $_f;
		}
		$countryData = array();
		$country = CountryModel::all();
		foreach($country as $_c){
			$countryData[$_c['id']] = $_c;
		}
		$makeData = array();
		$make = MakeModel::all();
		foreach($make as $_m){
			$makeData[$_m['id']] = $_m;
		}
		$seriesData = array();
		$series = SeriesModel::all();
		foreach($series as $_s){
			$seriesData[$_s['id']] = $_s;
		}
		$input = input('get.');
		if(isset($input['data'])){
			$supplier_id = Session::get('supplier_id');
			$input['supplier_id'] = $supplier_id;
			$data = $this->mator_obj->getSupplierMatorList($input);
			if(!empty($data['list']['data'])){
				foreach($data['list']['data'] as $key=>$val){
					$data['list']['data'][$key]['field_country_code'] = $fieldData[$val['field_id']]['field_code'].'-'.$countryData[$val['country_id']]['country_code'];
					$data['list']['data'][$key]['field_country_name'] = $fieldData[$val['field_id']]['field_name'].'-'.$countryData[$val['country_id']]['country_name'];
					$data['list']['data'][$key]['make_name'] = $makeData[$val['make_id']]['make_name'];
					$data['list']['data'][$key]['series_name'] = $seriesData[$val['series_id']]['series_name'];
					$data['list']['data'][$key]['year_name'] = '';
					if($seriesData[$val['series_id']]['from_time'] > 0){
						$data['list']['data'][$key]['year_name'] = date('Y/m',$seriesData[$val['series_id']]['from_time']).'~'.date('Y/m',$seriesData[$val['series_id']]['to_time']);
					}
					if(is_numeric($val['create_time'])){
						$data['list']['data'][$key]['create_time'] = date('Y-m-d H:i:s',$val['create_time']);
					}
				}
			}
			$result = array('code'=>0,'msg'=>'','count'=>$data['list']['total'],'data'=>$data['list']['data']);
			return json($result);
		}else{
			$where = array();
			$where['export'] = 1;
			$data = $this->country_obj->getFieldCountryList($where);
			$role_ids = Session::get('role_ids');
			if(!empty($data['list']) && !empty($role_ids)){
				foreach($data['list'] as $key=>$val){
					if(!in_array($val['id'],$role_ids)){
						unset($data['list'][$key]);
						continue;
					}
					$fieldObj = FieldModel::get($val['field_id']);
					$countryObj = CountryModel::get($val['country_id']);
					$data['list'][$key]['field_country_code'] = $fieldObj->field_code." - ".$countryObj->country_code;
					$data['list'][$key]['field_country_name'] = $fieldObj->field_name." - ".$countryObj->country_name;
				}
			}else{
				$data['list'] = array();
			}
			$this->assign('field_country',$data['list']);
			$this->assign('field', $fieldData);
			return view('index');
		}
    }
	
	/**
	 * 车型页面
	 * @return mixed
	 */
	public function small()
	{
		$input = input('get.');
		if(isset($input['data'])){	
			$result = array('code'=>0,'msg'=>'','count'=>0,'data'=>array());
			return json($result);
		}else{
			$where = array();
			$where['export'] = 1;
			$data = $this->country_obj->getFieldCountryList($where);
			$role_ids = Session::get('role_ids');
			if(!empty($data['list']) && !empty($role_ids)){
				foreach($data['list'] as $key=>$val){
					if(!in_array($val['id'],$role_ids)){
						unset($data['list'][$key]);
						continue;
					}
					$fieldObj = FieldModel::get($val['field_id']);
					$countryObj = CountryModel::get($val['country_id']);
					$data['list'][$key]['field_country_code'] = $fieldObj->field_code." - ".$countryObj->country_code;
					$data['list'][$key]['field_country_name'] = $fieldObj->field_name." - ".$countryObj->country_name;
				}
			}else{
				$data['list'] = array();
			}
			$this->assign('field_country',$data['list']);
			return view('small');
		}
	}
	
	/**
	 * 新增车型页面
	 * @return mixed
	 */
	public function add()
	{
		return view('add');
	}
	
	/**
	 * 编辑车型页面
	 * @return mixed
	 */
	public function edit()
	{
		$input = input('get.');
		$view = MatorModel::get($input['id']);
		$this->assign('view', $view);
		return view('edit');
	}
	
	/**
	 * 更新车型
	 * @return mixed
	 */
	public function save()
	{
		if ($this->request->isPost()) {
			$input = input('post.');
			if(isset($input['f_c_id'])){
				$where = array();
				$where['id'] = $input['f_c_id'];
				$data = $this->country_obj->getCountryRela($where);
				$input['field_id'] = $data[0]['field_id'];
				$input['country_id'] = $data[0]['country_id'];
			}
			$supplier_id = Session::get('supplier_id');
			$input['supplier_id'] = $supplier_id;
			if(isset($input['id']) && intval($input['id']) > 0){
				$flag = $this->mator_obj->save($input,['id'=>$input['id']]);
			}else{
				$input['create_time'] = time();
				$this->mator_obj->data($input);
				$flag = $this->mator_obj->save();
			}
			if($flag){
				$result = [
					'code' => 1,
					'msg'  => '请求成功',
					'list' => $input
				];
			}else{
				$result = [
					'code' => 0,
					'msg'  => '请求失败',
					'list' => $input
				];
			}
			return json($result);
		}else{
			$this->error('更新车型数据，请求不合法！');
		}
	}
	
	/**
	 * 删除车型
	 * @return mixed
	 */
	public function del()
	{
		if ($this->request->isPost()) {
			$input = input('post.');
			$where = array();
			$where['sku_status'] = 0;
			$where['mator_id'] = $input['id'];
			$series = $this->mator->getProductByMator($where);
			if(!empty($series)){
				$result = [
					'code' => 0,
					'msg'  => '车型已经关联产品，不允许删除！',
					'list' => $input
				];
			}else{
				$field = MatorModel::get($input['id']);
				$field->motor_status = -1;
				$flag = $field->save();
				if($flag){
					$result = [
						'code' => 1,
						'msg'  => '请求成功',
						'list' => $input
					];
				}else{
					$result = [
						'code' => 0,
						'msg'  => '请求失败',
						'list' => $input
					];
				}
			}
			return json($result);
		}else{
			$this->error('更新车型数据，请求不合法！');
		}
	}
	
	/**
	 * 更新车型状态
	 * @return mixed
	 */
	public function setStatus()
	{
		if ($this->request->isPost()) {
			$input = input('post.');
			$field = MatorModel::get($input['id']);
			$status = 1;
			if($field->mator_status){
				$status = 0;
			}
			$field->mator_status = $status;
			$flag = $field->save();
			if($flag){
				$result = [
					'code' => 1,
					'msg'  => '请求成功',
					'list' => $input
				];
			}else{
				$result = [
					'code' => 0,
					'msg'  => '请求失败',
					'list' => $input
				];
			}
			return json($result);
		}else{
			$this->error('更新车型数据，请求不合法！');
		}
	}
}
